/**************************************************************************
 //* W Corp. 
 //* W Profile Services                                             
 //* Package:  by.doubleyou.core.config
 //* (C) Copyright W Corp.  2007
 /**************************************************************************
 *
 * Copyright (c) 2007, W Corporation
 * All rights reserved.
 * 
 * Version: 1.0
 * Date:  3 Dec, 2007, Author: Alexander J. Perez Tchernov
 * 
 * MODIFICATION HISTORY:
 * Defect/Feature  	Description
 * ---
 */
package by.doubleyou.core.db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;

/**
 * Basic Storage
 * 
 * @author Alexander J. Perez Tchernov
 *
 */
public class Storage {
 

	//TODO remove
	private int maxConnections = 10;



	protected MiniConnectionPoolManager poolMgr;

	/**
	 * Get Connection Pool for the dataSource
	 * 
	 * @param lookupDataSource
	 * @return ConnectionPool (MiniConnectionPoolManager)
	 * @throws NamingException
	 */
	public MiniConnectionPoolManager setUpDataSource(ConnectionPoolDataSource dataSource)
			throws NamingException {
		return new MiniConnectionPoolManager(dataSource, maxConnections);
	}

	/**
	 * is table exist
	 * 
	 * @param tableName
	 * @return
	 * @throws SQLException
	 */
	public  boolean isTableExist(String tableName) throws SQLException {
		String[] names = { "TABLE" };
		ResultSet result = null;
		DatabaseMetaData metadata = null;

		Connection conn = getConnection();
		try {
			metadata = conn.getMetaData();
			result = metadata.getTables(null, null, null, names);
			boolean tableExists = false;
			while (result.next() && tableExists == false) {
				if (tableName.equals(result.getString("TABLE_NAME")))
					tableExists = true;
			}

			return tableExists;
		} finally {
			if (conn != null)
				conn.close();
		}
	}

	/**
	 * get Storage
	 * 
	 * @param lookupDataSource
	 * @return
	 * @throws Exception
	 */
	public static Storage getStorage(final ConnectionPoolDataSource datasource) throws NamingException {
		
		return  new Storage() {
			{
				this.poolMgr = setUpDataSource(datasource);
			}
		};
	}

 

	/**
	 * get connection
	 * 
	 * @return
	 */
	public Connection getConnection() throws SQLException {
		return poolMgr.getConnection();
	}

	/**
	 * dispose all unused connections
	 * 
	 * @throws SQLException
	 */
	public void dispose() throws SQLException {
		poolMgr.dispose();
	}

	/**
	 * safe close
	 * 
	 */
	public void closeSafe(Connection conn) {
		try {
			if (conn != null)
				conn.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	

	/**
	 * load embedded db
	 * 
	 * @param driver
	 */
	public static void load(String driver) {
		// --------- load embeded db ------------
		try {
			Class.forName(driver);
		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		}
		// --------- / load embeded db ------------
	}
}
